package com.hfjava.ebook.entity;

import java.io.Serializable;

/**
 * 分页类
 */
public class Pageable implements Serializable {
    private int totalCount = 0; //总记录数
    private int totalPage = 0; //总页数
    private int currPage = 1; //当前页码
    private int pageSize = 10; //页容量
    private Object list; //每页显示的数据记录
    private int start; //开始页码
    private int end; //结束页码

    public Pageable() {
    }

    public Pageable(int totalCount,int currPage, int pageSize, Object list) {
        this.totalCount = totalCount;
        this.totalPage = (int)Math.ceil((double)totalCount / this.pageSize);
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.list = list;

        /*
        1.判断当前页是否为第一页，不是，则添加上一页元素。
        2.遍历绘制页码，那么根据百度分页的逻辑，我们定义遍历的起始和结束变量start和end，总页码数totalPage，当前页码为currPage：
当总页码小于10的时候，start为1，end为totalPage；
总页码大于10，当前页码小于等于5，start为1，end为10；
总页码大于10，当前页码小于等于总页码数-4，start为totalPage-9，end为totalPage
总页码大于10，当前页介于上两个之间，start为currPage - 5，end为currPage + 4；


        3.判断当前页是否为最后一页，不是，则添加下一页元素。
        * */
        start = 1;
        end = totalPage;
        if (totalPage > 10) {
            if (currPage <= 5) {
                start = 1;
                end = 10;
            } else if (currPage >= totalPage -4) {
                start = totalPage - 9;
                end = totalPage;
            }else {
                start = currPage - 5 ;
                end  = currPage + 4;
            }
        }
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Object getList() {
        return list;
    }

    public void setList(Object list) {
        this.list = list;
    }

    public int getStart() {
        return this.start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return this.end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}
